package com.utooo.service.supplier;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.dao.supplier.AddressInfoMapper;
import com.utooo.dao.supplier.InvoiceBankInfoMapper;
import com.utooo.dao.supplier.SupplierArchiveMapper;
import com.utooo.dao.system.BankMapper;
import com.utooo.pojo.domain.supplier.AddressInfoDO;
import com.utooo.pojo.domain.supplier.InvoiceBankInfoDO;
import com.utooo.pojo.domain.supplier.SupplierArchiveDO;
import com.utooo.pojo.domain.system.BankDO;
import com.utooo.pojo.query.SupplierQuery;
import com.utooo.pojo.vo.supplier.SupplierListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author CK
 * @Date: 2025−04-17 17:44
 * @Description: SupplierArchiveServiceImpl
 * @Copyright: 南京优通信息科技股份有限公司
 */
@Service
@Slf4j
public class SupplierArchiveServiceImpl extends ServiceImpl<SupplierArchiveMapper, SupplierArchiveDO> implements SupplierArchiveService {

    @Resource
    private AddressInfoMapper addressInfoMapper;
    @Resource
    private BankMapper bankMapper;
    @Resource
    private InvoiceBankInfoMapper invoiceBankInfoMapper;

    @Override
    public List<SupplierListVO> getSupplierArchiveList(SupplierQuery supplierQuery) {
        String name = supplierQuery.getName();
        List<SupplierArchiveDO> list = this.list(new LambdaQueryWrapper<SupplierArchiveDO>()
                .like(StrUtil.isNotBlank(name), SupplierArchiveDO::getSupplierName, name));
        List<SupplierListVO> supplierList = BeanUtil.copyToList(list, SupplierListVO.class);
        supplierList.forEach(i -> {
            String supplierCode = i.getSupplierCode();
            LambdaQueryWrapper<AddressInfoDO> addressWrapper = new LambdaQueryWrapper<AddressInfoDO>()
                    .eq(AddressInfoDO::getSupplierCode, supplierCode)
                    .orderByDesc(AddressInfoDO::getCreateTime)
                    .last("limit 1");
            AddressInfoDO addressInfo = addressInfoMapper.selectOne(addressWrapper);
            if (addressInfo != null) {
                i.setAddress(addressInfo.getDetailedAddress());
                i.setPostalCode(addressInfo.getPostalCode());
            }
            LambdaQueryWrapper<InvoiceBankInfoDO> invoiceBankWrapper = new LambdaQueryWrapper<InvoiceBankInfoDO>()
                    .eq(InvoiceBankInfoDO::getSupplierCode, supplierCode)
                    .orderByDesc(InvoiceBankInfoDO::getCreateTime)
                    .last("limit 1");
            InvoiceBankInfoDO invoiceBankInfo = invoiceBankInfoMapper.selectOne(invoiceBankWrapper);
            if (invoiceBankInfo != null) {
                i.setBankName(invoiceBankInfo.getBankName());
                i.setBankAccount(invoiceBankInfo.getBankAccount());
                String bankCode = invoiceBankInfo.getBankCode();
                if (StrUtil.isNotBlank(bankCode)) {
                    LambdaQueryWrapper<BankDO> bankWrapper = new LambdaQueryWrapper<BankDO>()
                            .eq(BankDO::getId, bankCode);
                    BankDO bank = bankMapper.selectOne(bankWrapper);
                    if (bank != null) {
                        i.setBankAddress(bank.getAddress());
                        i.setBranchCode(bank.getBranchCode());
                    }
                }
            }
        });
        return supplierList;
    }
}
